package com.ht.pms.core.controller.MsgManagement;

import com.ht.pms.core.common.constant.SalaryConstants;
import com.ht.pms.core.dao.NoticeRecordDao;
import com.ht.pms.core.dao.SalarySheetManagementDao;
import com.ht.pms.core.dto.PageResult;
import com.ht.pms.core.dto.QueryNoticeRecordReq;
import com.ht.pms.core.dto.Result;
import com.ht.pms.core.dto.send.EmailResult;
import com.ht.pms.core.dto.send.NoticeEmailRecordDto;
import com.ht.pms.core.dto.upload.UpLoadTemp;
import com.ht.pms.core.dto.upload.UploadFile;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author:喻尊龙
 * @date: 2018/4/19
 */
//@CrossOrigin
@RestController
@RequestMapping("/msg")
public class MsgManagementController {

    private Logger logger = LoggerFactory.getLogger(MsgManagementController.class);

    @Autowired
    private NoticeRecordDao noticeRecordDao;
    @Autowired
    private SalarySheetManagementDao salarySheetManagementDao;

    @RequestMapping("/list")
    @ResponseBody
    public PageResult getMsgList(QueryNoticeRecordReq req){
        PageResult result = null;
        try{
            if(org.apache.commons.lang.StringUtils.isNotBlank(req.getDataTime())){
                SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH);
                Date d = sf.parse(req.getDataTime().substring(0,16));
                String time = new SimpleDateFormat("yyyy-MM").format(d);
                req.setDataTime(time);
            }
            List<UploadFile> uploadFiles = null;
            String templateCode = "";
            String batchCode = "";
            String tableName=SalaryConstants.RECORD_TABLE_YJ;
            String sendServer=req.getSendServer();
            if(StringUtils.isBlank(sendServer)){
                sendServer="1";
            }
//            uploadFiles = salarySheetManagementDao.getUploadFiles(req.getTemplateCode(),req.getDataTime());
//            if(uploadFiles != null && uploadFiles.size()>0){
//                templateCode = uploadFiles.get(0).getTemplateCode();
//                batchCode = uploadFiles.get(0).getBatchCode();
//                req.setBatchCode(batchCode);
//                req.setTemplateCode(templateCode);
//            }else {
//                result = PageResult.success(null,0);
//                return result;
//            }
            if ( "1".equals(sendServer) ) { //邮件
                tableName = SalaryConstants.RECORD_TABLE_YJ;
            } else if ("2".equals(sendServer)) { //工资
                tableName = SalaryConstants.RECORD_TABLE_DD;
            }
            List<EmailResult> noticeRecords = noticeRecordDao.queryNoticeRecords(req,tableName);
            if(!CollectionUtils.isEmpty(noticeRecords)){
                for (EmailResult emailResult :noticeRecords){
                    emailResult.setSendServer(sendServer);
                }
            }
            int count = noticeRecordDao.queryNoticeRecordsCount(req,tableName);
            result = PageResult.success(noticeRecords,count);
        }catch (Exception e){
            logger.error(e.getMessage());
            result = PageResult.error(500,"数据库查询异常"+e.getMessage());
        }

        return  result;
    }




}
